<template>
    <el-dialog :close-on-click-modal="false"  ref="dialogform" :title="title" :visible.sync="dialogVisible" width="30%"
        :before-close="handleClose">
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="120px" class="demo-ruleForm">
            <el-form-item :label="`${addEditName}数据项名称:`">
                <span class="primary"> {{ ruleForm.dataTypeName }}</span>
            </el-form-item>
            <el-form-item label="单位:`">
                <el-input v-model="ruleForm.unitNo"></el-input>
            </el-form-item>
            <!-- <el-form-item label="上传图标">
                <UpLoadImage></UpLoadImage>
                <el-button type="text">选择系统图标</el-button>
            </el-form-item> -->
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="cancel">取 消</el-button>
            <el-button type="primary" @click="submitForm">确 定</el-button>
        </span>
    </el-dialog>
</template>
<script>
import UpLoadImage from "@/components/UpLoadImage";
import dataClassManagement from '@/api/basicInformation/dataClassManagement'
export default {
    components: { UpLoadImage },
    props: {
        addEditFlag: {
            type: String,
            default: () => {
                return 'add'
            }
        },
        data: {
            type: Object,
            default: () => {
                return {}
            }
        },
        title: {
            type: String,
            default: () => {
                return '操作表单'
            }
        },
        dialogVisible: {
            type: Boolean,
            default: () => {
                return false
            }
        }
    },
    data() {
        return {
            addEditName: '添加',
            addEditApi: dataClassManagement.addlist,
            ruleForm: {
            },
            rules: {
                // item1: [
                //     { required: true, message: '请上传图标', trigger: 'change' }
                // ]
            }
        }
    },
    computed: {

    },
    watch: {
        dialogVisible(v) {
            v && this.initData()
        }
    },
    
    mounted() { },
    methods: {
        initData() {
            if (this.addEditFlag == 'add') {
                this.resetForm();
                this.addEditApi = dataClassManagement.addlist;
                this.addEditName = '分配';
            } else if (this.addEditFlag == 'edit') {
                this.addEditApi = dataClassManagement.editlist;
                this.addEditName = '取消';
            };
            this.ruleForm = { ...this.data };
        },
        //提交表单
        submitForm() {
            this.$refs.ruleForm.validate((valid) => {
                if (valid) {
                    this.addEditApi({ ...this.ruleForm }).then(res => {
                        this.cancel()
                    }).catch(err => {
                        this.$message.warning('操作失败')
                    })
                } else {
                    return false;
                }
            })
        },
        //重置
        resetForm() {
            this.ruleForm = {};
            this.$refs.ruleForm && this.$refs.ruleForm.resetFields()
        },
        //取消关闭
        cancel() {
            this.resetForm()
            this.$emit("closeDialog")
        },
        handleClose(done) {
            this.$confirm('确认关闭？')
                .then(_ => { this.resetForm(); this.cancel() })
                .catch(_ => { });
        }
    }
};
</script>
<style lang="scss" scoped></style>
  